#include "gamecontrol.h"
#include<QTime>
GameControl::GameControl(QObject *parent) : QObject(parent)
{

}

void GameControl::playerInit()
{
    //对象实例化
    m_robotLeft=new Robot("机器人A",this);
    m_robotRight=new Robot("机器人B",this);
    m_user=new  UserPlayer("我自己",this);

    //玩家头像显示方位
    m_robotLeft->setDirection(Player::Left);
    m_robotRight->setDirection(Player::Right);

    //性别
    Player::Sex sex;
    qsrand(QTime::currentTime().msec());
    int num=qrand()%2;
    sex=(Player::Sex)num;
    m_robotLeft->setSex(sex);

     num=qrand()%2;
    sex=(Player::Sex)num;
    m_robotRight->setSex(sex);

     num=qrand()%2;
    sex=(Player::Sex)num;
    m_user->setSex(sex);

    //出牌顺序
    //user
    m_user->setPrev(m_robotLeft);
    m_user->setNext(m_robotRight);

    //left
    m_robotLeft->setPrev(m_robotRight);
    m_robotLeft->setNext(m_user);

    //right
    m_robotRight->setPrev(m_user);
    m_robotRight->setNext(m_robotLeft);


    //指定当前玩家
    m_currPlayer=m_user;

    //处理玩家发射出的信号
    connect(m_user,&UserPlayer::notifyGrabLordBet,this,&GameControl::onGrabBet);
    connect(m_robotLeft,&UserPlayer::notifyGrabLordBet,this,&GameControl::onGrabBet);
    connect(m_robotRight,&UserPlayer::notifyGrabLordBet,this,&GameControl::onGrabBet);

}

Robot *GameControl::getLeftRobot()
{
    return m_robotLeft;
}

Robot *GameControl::getRightRobot()
{
    return m_robotRight;
}

UserPlayer *GameControl::getUserPlayer()
{
    return m_user;
}

Player *GameControl::getCurrPlayer() const
{
    return m_currPlayer;
}

void GameControl::setCurrPlayer(Player *currPlayer)
{
    m_currPlayer = currPlayer;
}

Player *GameControl::getPendPlayer()
{
    return m_pendPlayer;
}

Cards GameControl::getPendCards()
{
    return m_pendCards;
}

void GameControl::initAllCards()
{
    m_allCards.clear();
    for (int p = static_cast<int>(Card::Card_Begin) + 1; p < static_cast<int>(Card::Card_SJ); ++p) {
                for (int s = static_cast<int>(Card::suit_Begin) + 1; s < static_cast<int>(Card::Suit_End); ++s) {
                    Card c(static_cast<Card::CardPoint>(p), static_cast<Card::CardSuit>(s));
                    m_allCards.add(c);
                }
            }
    Card card(Card::Card_SJ, Card::suit_Begin);
    m_allCards.add(card);
    Card card1(Card::Card_BJ, Card::suit_Begin);
    m_allCards.add(card1);
}

Card GameControl::takeOneCard()
{
    return m_allCards.takeRandCard();
}

Cards GameControl::getSurplusCards()
{
    return m_allCards;
}

void GameControl::resetCardData()
{
    //洗牌
    initAllCards();
    //清空玩家所有牌
    m_robotLeft->clearCards();
    m_robotRight->clearCards();
    m_user->clearCards();

    //初始化出牌玩家和牌
    m_pendPlayer= nullptr;
    m_pendCards.clear();
}

void GameControl::startLordCard()
{
    m_currPlayer->prepareCallLord();
    emit playerStatusChanged(m_currPlayer,ThinkingForCallLord);
}

void GameControl::becomeLord(Player*player)
{
    player->setRole(Player::Lord);
    player->prev()->setRole(Player::Farme);
    player->next()->setRole(Player::Farme);

    m_currPlayer=player;
    player->storeDispatchCard(m_allCards);

    QTimer::singleShot(1000,this,[=](){
       emit gameStatusChanged(PlayingHand);
       emit playerStatusChanged(player,ThinkingForPlayHand);
       m_currPlayer->preparePlayHand();
    });

}

void GameControl::clearPlayerScore()
{
    m_robotLeft->setScore(0);
    m_robotRight->setScore(0);
    m_user->setScore(0);
}

int GameControl::getPlayerMaxBet()
{
    return m_betRecord.bet;
}

void GameControl::onGrabBet(Player *player, int bet)
{
    //1.通知主界面玩家叫地主（更新信息提示）
    if(bet==0||m_betRecord.bet>=bet)
    {
        emit notifyGrabLordBet(player,0,false);
    }
    else  if(bet>0&&m_betRecord.bet==0)
    {
        //第一个抢地主的人
        emit notifyGrabLordBet(player,bet,true);
    }
    else
    {
        //第二个或者第三个抢地主的玩家
        emit notifyGrabLordBet(player,bet,false);
    }
//    emit notifyGrabLordBet(player,bet);
    //2.判断玩家下注是不是3分，如果是抢地主结束
    if(bet==3)
    {
        //玩家成为地主
        becomeLord(player);
        //清空数据
        m_betRecord.reset();
    }
    //3.下注不够3分，对玩家的分数进行比较，分数高的是地主
    if(m_betRecord.bet<bet)
    {
        m_betRecord.bet=bet;
        m_betRecord.player=player;
    }
    m_betRecord.times++;
    //如果每个玩家都抢过一次了，结束抢地主
    if(m_betRecord.times==3)
    {
        if(m_betRecord.bet==0)
        {
            emit gameStatusChanged(DispatchCard);
        }
        else {
            becomeLord(m_betRecord.player);
        }
        m_betRecord.reset();
    }

    //4。切换玩家，通知下一个玩家继续抢地主
    m_currPlayer=player->next();
    //发生信号给主界面，告知当前状态为抢地主状态
    emit  playerStatusChanged(m_currPlayer,ThinkingForCallLord);
    m_currPlayer->prepareCallLord();

}
